package com.seio.edc.controller.system;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.nova.edc.model.system.Login;
import com.nova.edc.service.system.LoginService;
import com.nova.edc.service.system.RoleSystemMenuService;
import com.seio.edc.common.MD5;
import com.seio.edc.controller.core.BaseContoller;

@Controller
@RequestMapping(value = "/system")
public class SystemMenuContronller extends BaseContoller {

	@Autowired
	private LoginService loginService;
	@Autowired
	private com.nova.edc.service.system.LoginService novaLoginService;
	@Autowired
	private RoleSystemMenuService novaRoleSystemMenuService;

	@RequestMapping(value = "systemMenu.shtml")
	public ModelAndView systemMenu(HttpServletRequest request,
			HttpSession session, ModelMap modelMap) {
		Integer roleid = (Integer) session.getAttribute("roleid");
		modelMap.put("systemMenus",
				novaRoleSystemMenuService.findMenuByRole(roleid));
		return new ModelAndView("system/systemMenu", modelMap);
	}

	@InitBinder("loginSearchEnity")
	public void initBinder2(WebDataBinder binder) {
		binder.setFieldDefaultPrefix("loginSearchEnity.");
	}

	@InitBinder("loginEntity")
	public void initBinder1(WebDataBinder binder) {
		binder.setFieldDefaultPrefix("loginEntity.");
	}

	@RequestMapping(value = "userNameVali.shtml")
	@ResponseBody
	public Map<String, Object> userNameVali(HttpServletRequest request,
			String userName, Integer loginId) {
		Map<String, Object> map = new HashMap<String, Object>();
		// String userName = (String) param.get("userName");
		// Integer loginId = (Integer) param.get("loginId");
		map.put("existFlag", novaLoginService.isExistLogin(userName, loginId));
		return map;
	}

	@RequestMapping(value = "versionVali.shtml")
	@ResponseBody
	public Map<String, Object> versionVali(HttpServletRequest request,
			@RequestBody HashMap<String, String> param) {

		Map<String, Object> map = new HashMap<String, Object>();

		Login loginEntity = novaLoginService.getById(Integer.valueOf(param
				.get("id")));

		if (loginEntity.getModifystamp() != null
				&& loginEntity.getModifystamp().toString()
						.equals(param.get("modifyStamp"))) {
			map.put("versionFlag", true);
		} else {
			map.put("versionFlag", false);
		}

		return map;
	}

	@RequestMapping(value = "changePassword.shtml")
	public ModelAndView changePassword(HttpServletRequest request,
			ModelMap modelMap) {
		return new ModelAndView("system/changePassword", modelMap);
	}

	@RequestMapping(value = "oldPasswordVali.shtml")
	@ResponseBody
	public Map<String, Object> oldPasswordVali(HttpServletRequest request,
			@RequestBody String oldp) {

		Map<String, Object> map = new HashMap<String, Object>();

		Login loginEntity = (Login) request.getSession().getAttribute(
				"sessionLogin");

		if (loginEntity != null
				&& MD5.Md5(oldp).equals(loginEntity.getPassword())) {
			map.put("oldPasswordFlag", true);
		} else {
			map.put("oldPasswordFlag", false);
		}

		return map;
	}

	@RequestMapping(value = "updatePassword.shtml")
	@ResponseBody
	public Map<String, Object> changePassword(HttpServletRequest request,
			@RequestBody String newP) {

		Map<String, Object> map = new HashMap<String, Object>();
		Login loginEntity = (Login) request.getSession().getAttribute(
				"sessionLogin");
		loginEntity.setPassword(MD5.Md5(newP));
		int flag = loginService.update(loginEntity);

		if (flag == 0) {
			map.put("flag", true);
		} else {
			map.put("flag", false);
		}

		return map;
	}
}
